import {
  getofficesale,
  getofficehire,
  getshopssale,
  getshopshire,
} from '@/api/commerce';
import { useAddKey } from '@/hooks/useKey';

const IndexModel = {
  namespace: 'commerce',

  state: {
    officesale: [],
    searchofficesale: [],
    officehire: [],
    searchofficehire: [],
    shopssale: [],
    searchshopssale: [],
    shopshire: [],
    searchshopshire: [],
  },

  effects: {
    *getofficesale({ payload }, { call, put }) {
      const data = yield getofficesale();
      // console.log(data.data.data);
      yield put({ type: 'setofficesale', payload: data.data.data });
    },
    *getofficehire({ payload }, { call, put }) {
      const data = yield getofficehire();
      // console.log(data.data.data);
      yield put({ type: 'setofficehire', payload: data.data.data });
    },
    *getshopssale({ payload }, { call, put }) {
      const data = yield getshopssale();
      // console.log(data.data.data);
      yield put({ type: 'setshopssale', payload: data.data.data });
    },
    *getshopshire({ payload }, { call, put }) {
      const data = yield getshopshire();
      // console.log(data.data.data);
      yield put({ type: 'setshopshire', payload: data.data.data });
    },
  },
  reducers: {
    setofficesale(state, { payload }) {
      let list = useAddKey(payload);
      console.log(list);
      return {
        ...state,
        officesale: list,
        searchofficesale: list,
      };
    },
    delofficesale(state, { payload }) {
      let houselist = JSON.parse(JSON.stringify(state.officesale));
      let ind = houselist.findIndex((item) => item.id === payload);
      houselist.splice(ind, 1);
      return {
        ...state,
        officesale: houselist,
      };
    },
    setofficehire(state, { payload }) {
      let list = useAddKey(payload);
      // console.log(list);
      return {
        ...state,
        officehire: list,
        searchofficehire: list,
      };
    },
    delofficehire(state, { payload }) {
      let houselist = JSON.parse(JSON.stringify(state.officehire));
      let ind = houselist.findIndex((item) => item.id === payload);
      houselist.splice(ind, 1);
      return {
        ...state,
        officehire: houselist,
      };
    },
    setshopssale(state, { payload }) {
      let list = useAddKey(payload);
      return {
        ...state,
        shopssale: list,
        searchshopssale: list,
      };
    },
    delshopssale(state, { payload }) {
      let houselist = JSON.parse(JSON.stringify(state.shopssale));
      let ind = houselist.findIndex((item) => item.id === payload);
      houselist.splice(ind, 1);
      return {
        ...state,
        shopssale: houselist,
      };
    },
    setshopshire(state, { payload }) {
      let list = useAddKey(payload);
      return {
        ...state,
        shopshire: list,
        searchshopshire: list,
      };
    },
    delshopshire(state, { payload }) {
      let houselist = JSON.parse(JSON.stringify(state.shopshire));
      let ind = houselist.findIndex((item) => item.id === payload);
      houselist.splice(ind, 1);
      return {
        ...state,
        shopshire: houselist,
      };
    },
    searchofficesale(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchofficesale));
      list = list.filter((item) => {
        return item.title.includes(payload);
      });
      return {
        ...state,
        officesale: list,
      };
    },
    searchofficehire(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchofficehire));
      list = list.filter((item) => {
        return item.estate_name.includes(payload);
      });
      return {
        ...state,
        officehire: list,
      };
    },
    searchshopssale(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchshopssale));
      list = list.filter((item) => {
        return item.estate_name.includes(payload);
      });
      return {
        ...state,
        shopssale: list,
      };
    },
    searchshopshire(state, { payload }) {
      let list = JSON.parse(JSON.stringify(state.searchshopshire));
      list = list.filter((item) => {
        return item.estate_name.includes(payload);
      });
      return {
        ...state,
        shopshire: list,
      };
    },
    editofficesale(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.officesale));
      const { id, values } = payload;
      if (id) {
        let ind = sands.findIndex((item) => item.id === id);
        sands[ind] = { ...sands[ind], ...values };
        return {
          ...state,
          officesale: sands,
        };
      } else {
        values.id = state.officesale[state.officesale.length - 1].id + 1;
        values.key = values.id;
        sands.push({ ...values });
        console.log(sands);
        return {
          ...state,
          officesale: sands,
        };
      }
    },
    editofficehire(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.officehire));
      const { id, values } = payload;
      if (id) {
        let ind = sands.findIndex((item) => item.id === id);
        sands[ind] = { ...sands[ind], ...values };
        return {
          ...state,
          officehire: sands,
        };
      } else {
        values.id = state.officehire[state.officehire.length - 1].id + 1;
        values.key = values.id;
        sands.push({ ...values });
        console.log(sands);
        return {
          ...state,
          officehire: sands,
        };
      }
    },
    editshopssale(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.shopssale));
      const { id, values } = payload;
      if (id) {
        let ind = sands.findIndex((item) => item.id === id);
        sands[ind] = { ...sands[ind], ...values };
        return {
          ...state,
          shopssale: sands,
        };
      } else {
        values.id = state.shopssale[state.shopssale.length - 1].id + 1;
        values.key = values.id;
        sands.push({ ...values });
        console.log(sands);
        return {
          ...state,
          shopssale: sands,
        };
      }
    },
    editshopshire(state, { payload }) {
      let sands = JSON.parse(JSON.stringify(state.shopshire));
      const { id, values } = payload;
      if (id) {
        let ind = sands.findIndex((item) => item.id === id);
        sands[ind] = { ...sands[ind], ...values };
        return {
          ...state,
          shopshire: sands,
        };
      } else {
        values.id = state.shopshire[state.shopshire.length - 1].id + 1;
        values.key = values.id;
        sands.push({ ...values });
        console.log(sands);
        return {
          ...state,
          shopshire: sands,
        };
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      console.log(111222);
      //   dispatch({type:'commerce/getofficehire'})
      // dispatch({type:'commerce/getofficesale'})
      // dispatch({type:'commerce/getshopshire'})
      // dispatch({type:'commerce/getshopssale'})
    },
  },
};

export default IndexModel;
